function TemplateProperties(mode)
{

var oTemplateObj = new TemplateObj(crmForm.title.value, crmForm.description.value, crmForm.languagecode.value);

oTemplateObj = openStdDlg(prependOrgName("/Tools/KBTemplateEditor/Dialogs/kbtemplateproperties.aspx?mode=" + mode), oTemplateObj, 400, 300);

if (oTemplateObj)
{
crmForm.title.value			= oTemplateObj.Title;
crmForm.description.value	= oTemplateObj.Description;
crmForm.languagecode.value	= oTemplateObj.LanguageCode;

UpdateFormData();
}
else if (mode == "new")
{
alert(LOCID_NO_ARTICLE_TEMPLATE);
crmForm._bSaving = true;
window.close();
}
}

function AddSection()
{
var oSectionObj = openStdDlg(prependOrgName("/Tools/KBTemplateEditor/Dialogs/section.aspx?mode=new"), null, 400, 280);

if (oSectionObj)
{
var oSections = oStructureXml.selectSingleNode("/kbarticle/sections");

var id = 0;




var oNextSectionIdAttr = oSections.attributes.getNamedItem("nextSectionId");
if (oNextSectionIdAttr)
{
id = parseInt(oNextSectionIdAttr.value, 10);
}
else
{
id = CalculateNextSectionId();
}



oSections.setAttribute("nextSectionId", id+1);




var oSectionNode = oStructureXml.createElement("section");
oSectionNode.setAttribute("type", "edit");
oSectionNode.setAttribute("id", id);
oSectionNode.appendChild(oStructureXml.createCDATASection(oSectionObj.Title));

oSections.appendChild(oSectionNode);

if (oSectionObj.Instructions)
{
var oInstNode = oStructureXml.createElement("instructions");
oInstNode.appendChild(oStructureXml.createCDATASection(oSectionObj.Instructions));
oSectionNode.appendChild(oInstNode);
}



var oRow	= editor.firstChild.firstChild.insertRow();
var oCell	= oRow.insertCell();

oCell.className = "section";



oCell.attachEvent("onclick", SetActiveObject);
oCell.attachEvent("ondblclick", ViewPropertiesNow);

var oTitleDiv = document.createElement("div");
oCell.appendChild(oTitleDiv);

var oBodyDiv = document.createElement("div");
oBodyDiv.className = "article";
oBodyDiv.innerHTML = CrmEncodeDecode.CrmHtmlEncode(LOCID_USE_STYLE_BUTTONS) + "<br><br>";
oCell.appendChild(oBodyDiv);

oCell.sectionId = id;

oTitleDiv.className = "heading";
oTitleDiv.innerText	= oSectionObj.Title;

oCell.title = oSectionObj.Instructions;

oCell.click();

UpdateFormData();
}
}

function UpdateSection()
{
if (_oActive)
{
var oSectionObj = new SectionObj(_oActive.firstChild.innerText, _oActive.title);

var oUpdatedSectionObj = openStdDlg(prependOrgName("/Tools/KBTemplateEditor/Dialogs/section.aspx?mode=edit"), oSectionObj, 400, 280);

if (oUpdatedSectionObj)
{


var oNode = GetSectionNode();
oNode.childNodes.item(0).text = oUpdatedSectionObj.Title;



_oActive.firstChild.innerText	= oUpdatedSectionObj.Title;
_oActive.title = oUpdatedSectionObj.Instructions;

var oInst = oNode.selectSingleNode("instructions");

if (oUpdatedSectionObj.Instructions)
{
if (oInst)
{
oInst.childNodes.item(0).text = oUpdatedSectionObj.Instructions;
}
else
{
var oInstNode = oStructureXml.createElement("instructions");
oInstNode.appendChild(oStructureXml.createCDATASection(oUpdatedSectionObj.Instructions));
oNode.appendChild(oInstNode);
}
}
else
{
if (oInst)
{
oNode.removeChild(oInst);
}
}

UpdateFormData();
}
}
}

function Remove()
{
if (_oActive && confirm(LOCID_CONFIRM_REMOVE))
{



var oSections = oStructureXml.selectSingleNode("/kbarticle/sections");

var oNextSectionIdAttr = oSections.attributes.getNamedItem("nextSectionId");

if (!oNextSectionIdAttr)
{
var id = CalculateNextSectionId();
oSections.setAttribute("nextSectionId", id);
}



var oNode = GetSectionNode();

oNode.parentNode.removeChild(oNode);



_oActive.parentElement.parentElement.deleteRow(_oActive.parentElement.rowIndex);

_oActive = null;

UpdateFormData();
}
}

function CalculateNextSectionId()
{


var id = 0;
var sectionId = 0;

var oEditSections = oStructureXml.selectNodes("/kbarticle/sections/section[@type='edit']");
if (oEditSections)
{
id = oEditSections.length;
for (var n = 0; n < oEditSections.length; n++)
{
if (oEditSections[n].attributes.getNamedItem("id"))
{
sectionId = parseInt(oEditSections[n].attributes.getNamedItem("id").value, 10);
}

id = (sectionId >= id) ? sectionId + 1 : id;
}
}

return id;
}

function SetActiveObject()
{
var o = event.srcElement;

if (_oActive)
{
_oActive.style.border = "1px dashed #cccccc";
}

while (o.tagName != "TD")
{
o = o.parentElement;
}

o.style.border = "1px solid #23b30c";

_oActive = o;
}

function ViewPropertiesNow()
{
var o = event.srcElement;

while (o.tagName != "TD")
{
o = o.parentElement;
}

_oActive = o;

UpdateSection();
}

function Move(bUp)
{
if (_oActive)
{
bUp ? MoveUp() : MoveDown();
}
}

function MoveUp()
{
if (_oActive.parentElement.rowIndex > 2)
{


var oNode = GetSectionNode();
var oParentNode = oNode.parentNode;

oParentNode.insertBefore(oParentNode.removeChild(oNode.previousSibling), oNode.nextSibling);



_oActive.parentElement.swapNode(_oActive.parentElement.previousSibling);

UpdateFormData();
}
}

function MoveDown()
{
if (_oActive.parentElement.rowIndex < _oActive.parentElement.parentElement.parentElement.rows.length - 1)
{


var oNode = GetSectionNode();

var oParentNode = oNode.parentNode;

oParentNode.insertBefore(oParentNode.removeChild(oNode.nextSibling), oNode);



_oActive.parentElement.swapNode(_oActive.parentElement.nextSibling);

UpdateFormData();
}
}

function UpdateFormData()
{
crmForm.structurexml.value = Trim(oStructureXml.xml);
}

function GetSectionNode()
{
return oStructureXml.selectSingleNode("kbarticle/sections/section[@id = '" + _oActive.sectionId + "']");
}
